angular.module('ms', ['ksSwiper'])
    .controller('priceList',
    [
        "$scope", "$http", "$compile", function ($scope, $http, $compile) {
            $scope.orderId = imi.GetQueryString("order_id");
            $scope.token = imi.GetQueryString("token");
            //基本信息
            $scope.basicInfo = null;
            $scope.hotelList = [];
            $scope.basicValidate = {
                total: true,
                order_date: true,
                order_type: true,
                staff_hotel_id: true,
                order_place: true,
                groom_name: true,
                bride_name: true
            }

            //文字主题
            $scope.curTextTheme = null;
            $scope.textThemeList = [];
            $scope.textWords = null;
            $scope.textRemark = null;
            $scope.textThemeValidate = {
                total: true,
                textWords: true,
                textRemark: true
            }

            //颜色主题
            $scope.curColorTheme = null;
            $scope.colorThemeList = [];
            $scope.colorName = null;
            $scope.colorRemark = null;
            $scope.mainColor = null;
            $scope.secondColor = null;
            $scope.thirdColor = null;
            $scope.colorThemeValidate = {
                total: true,
                colorName: true,
                colorRemark: true,
                mainColor: true,
                secondColor: true,
                thirdColor: true
            }

            //仪式区效果图
            $scope.curYsqList = [];
            $scope.ysqList = [];
            $scope.ysqImg = null;
            $scope.ysqDesc = null;
            $scope.ysqValidate = {
                total: true,
                ysqImg: true,
                ysqDesc: true
            }

            //迎宾区效果图
            $scope.curYbqList = [];
            $scope.ybqList = [];
            $scope.ybqImg = null;
            $scope.ybqDesc = null;

            $scope.PopConfirm = null;//弹窗确定操作

            //页面初始化
            $scope.Init = function () {
                $scope.curYsqList = [
                    {
                        id: 1,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: "images/ad.jpg",
                        desc: "123123"
                    }, {
                        id: 2,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: "images/ad.jpg",
                        desc: "456456"
                    }
                ];
                $scope.ysqList = [];
                $scope.curYbqList = [
                    {
                        id: 1,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: "images/ad.jpg",
                        desc: "123123"
                    }, {
                        id: 2,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: "images/ad.jpg",
                        desc: "456456"
                    }
                ];
                $scope.ybqList = [
                    {
                        id: 1,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: "images/ad.jpg",
                        desc: "123123",
                        checked: true
                    }, {
                        id: 2,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: "images/ad.jpg",
                        desc: "456456",
                        checked: true
                    }
                ];

                new Swiper($(".effect_swiper"), {
                    slidesPerView: 'auto',
                    initialSlide: 0,
                    autoPlay: false,
                    loop: false,
                    nextButton: '.swiper-button-next',
                    prevButton: '.swiper-button-prev',
                });

                $http
                    .post("http://crm.cike360.com/portal/index.php?r=background/Price_list_data",
                        JSON.stringify({
                            order_id: $scope.orderId,
                            token: $scope.token
                        }))
                    .success(function (data) {
                        //console.log(JSON.stringify(data));
                        $scope.basicInfo = data.result.order_data;
                        $scope.hotelList = data.hotel_list;
                        $scope.textThemeList = data.words_list;
                        angular.forEach($scope.textThemeList,
                            function (i, x) {
                                if (i.selected == true) {
                                    $scope.curTextTheme = i;
                                    return;
                                }
                            });
                        $scope.colorThemeList = data.color_list;
                        angular.forEach($scope.colorThemeList,
                            function (i, x) {
                                if (i.selected == true) {
                                    $scope.curColorTheme = i;
                                    return;
                                }
                            });
                    })
                .error(function (data, status, headers, config) {
                    alert("网络出现问题，请稍后重试！");
                });
            }

            $scope.Init();

            //基本信息
            //编辑
            $scope.OpenBasicPop = function () {
                bombBox({
                    entrance: $('.base_msgbox')
                });
                $("#order_date").jcDate({
                    IcoClass: "jcDateIco",
                    Event: "click",
                    Speed: 100,
                    Left: 0,
                    Top: 28,
                    format: "-",
                    Timeout: 100
                });
                //提交
                $scope.PopConfirm = function ($event) {
                    $scope.basicInfo.order_type = 1;
                    if ($scope.BasicValidate()) {
                        $http
                            .post("http://crm.cike360.com/portal/index.php?r=background/Edit_order_info",
                                JSON.stringify({
                                    token: $scope.token,
                                    orderplace: $scope.basicInfo.order_place,
                                    orderdate: $scope.basicInfo.order_date,
                                    ordertype: $scope.basicInfo.order_type,
                                    guest_amount: "",
                                    wedding_style: "",
                                    hotelid: $scope.basicInfo.staff_hotel_id,
                                    groomname: $scope.basicInfo.groom_name,
                                    groomtelephone: $scope.basicInfo.groom_phone,
                                    bridename: $scope.basicInfo.bride_name,
                                    bridetelephone: $scope.basicInfo.bride_phone,
                                    linkmanname: $scope.basicInfo.contact_name,
                                    linkmantelephone: $scope.basicInfo.contact_phone,
                                    remark: "",
                                    product: [],
                                    orderId: $scope.orderId
                                }))
                            .success(function (data) {
                                alert(data);
                                //$($event.target).parents(".msgbox").hide();
                            })
                            .error(function (data, status, headers, config) {
                                alert("网络出现问题，请稍后重试！");
                            });
                    }
                }
            }
            //基本信息验证
            $scope.BasicValidate = function () {
                if ($.trim($scope.basicInfo.order_date) == "") {
                    $scope.basicValidate.order_data = false;
                } else {
                    $scope.basicValidate.order_data = true;
                }
                if ($.trim($scope.basicInfo.order_type) == "") {
                    $scope.basicValidate.order_type = false;
                } else {
                    $scope.basicValidate.order_type = true;
                }
                if ($.trim($scope.basicInfo.staff_hotel_id) == "") {
                    $scope.basicValidate.staff_hotel_id = false;
                } else {
                    $scope.basicValidate.staff_hotel_id = true;
                }
                if ($.trim($scope.basicInfo.order_place) == "") {
                    $scope.basicValidate.order_place = false;
                } else {
                    $scope.basicValidate.order_place = true;
                }
                if ($.trim($scope.basicInfo.groom_name) == "") {
                    $scope.basicValidate.groom_name = false;
                } else {
                    $scope.basicValidate.groom_name = true;
                }
                if ($.trim($scope.basicInfo.bride_name) == "") {
                    $scope.basicValidate.bride_name = false;
                } else {
                    $scope.basicValidate.bride_name = true;
                }
                $scope.basicValidate.total = $scope.basicValidate.order_data &&
                    $scope.basicValidate.order_type &&
                    $scope.basicValidate.staff_hotel_id &&
                    $scope.basicValidate.order_place &&
                    $scope.basicValidate.groom_name &&
                    $scope.basicValidate.bride_name;
                return $scope.basicValidate.total;
            }
            $scope.OrderTypeSelected = function (id) {
                $scope.basicInfo.order_type = id;
            }
            $scope.HotelSelected = function (id) {
                $scope.basicInfo.staff_hotel_id = id;
            }

            //文字主题
            $scope.OpenTextThemePop = function () {
                bombBox({
                    entrance: $('.theme_word_msgbox')
                });
            }
            //添加文字主题
            $scope.AddTextTheme = function () {
                bombBox({
                    title: '添加主题文字',
                    entrance: $('.text_theme_add_msgbox'),
                    initFun: function () {
                        $scope.textWords = "";
                        $scope.textRemark = "";
                    }
                });

                $scope.PopConfirm = function ($event) {
                    if ($scope.TextThemeValidate()) {
                        $http
                            .post("http://crm.cike360.com/portal/index.php?r=background/Insert_word_theme",
                                JSON.stringify({
                                    words: $scope.textWords,
                                    remark: $scope.textRemark,
                                    token: $scope.token
                                }))
                            .success(function (data) {
                                alert(data);
                                $scope.textThemeList.push({
                                    id: 1000,
                                    words: $scope.textWords,
                                    remark: $scope.textRemark,
                                    selected: false
                                });
                                $($event.target).parents(".msgbox").hide();
                            })
                            .error(function (data, status, headers, config) {
                                alert("网络出现问题，请稍后重试！");
                            });
                    }
                }
            }
            //编辑文字主题
            $scope.EditTextTheme = function (item) {
                bombBox({
                    title: '编辑文字主题',
                    entrance: $('.text_theme_add_msgbox'),
                    initFun: function () {
                        $scope.textWords = item.words;
                        $scope.textRemark = item.remark;
                    }
                });
                $scope.PopConfirm = function ($event) {
                    if ($scope.TextThemeValidate()) {
                        $http
                            .post("http://crm.cike360.com/portal/index.php?r=background/edit_word_theme",
                                JSON.stringify({
                                    words: $scope.textWords,
                                    remark: $scope.textRemark,
                                    word_id: item.id
                                }))
                            .success(function (data) {
                                alert(data);
                                angular.forEach($scope.textThemeList,
                                    function (i, t) {
                                        if (i.id == item.id) {
                                            i.words = $scope.textWords;
                                            i.remark = $scope.textRemark;
                                        }
                                    });
                                if ($scope.curTextTheme.id == item.id) {
                                    $scope.curTextTheme.words = $scope.textWords;
                                    $scope.curTextTheme.remark = $scope.textRemark;
                                }
                                $($event.target).parents(".msgbox").hide();
                            })
                            .error(function (data, status, headers, config) {
                                alert("网络出现问题，请稍后重试！");
                            });
                    }
                }
            }
            //删除文字主题
            $scope.DeleteTextTheme = function (id) {
                $http
                    .post("http://crm.cike360.com/portal/index.php?r=background/del_word_theme",
                        JSON.stringify({
                            word_id: id
                        }))
                    .success(function (data) {
                        alert(data);
                        angular.forEach($scope.textThemeList,
                            function (i, x) {
                                if (i.id == id) {
                                    $scope.textThemeList.splice(x, 1);
                                    return;
                                }
                            });
                        if ($scope.curTextTheme.id == id) {
                            $scope.curTextTheme = null;
                        }
                    })
                    .error(function (data, status, headers, config) {
                        alert("网络出现问题，请稍后重试！");
                    });
            }
            //删除已选文字主题
            $scope.DeleteSelectedTextTheme = function (id) {
                $http
                    .post("http://crm.cike360.com/portal/index.php?r=background/del_word_theme_order_show",
                        JSON.stringify({
                            show_id: id
                        }))
                    .success(function (data) {
                        alert(data);
                        angular.forEach($scope.textThemeList,
                            function (i, x) {
                                if (i.id == id) {
                                    i.selected = false;
                                    return;
                                }
                            });
                        $scope.curTextTheme = null;
                    })
                    .error(function (data, status, headers, config) {
                        alert("网络出现问题，请稍后重试！");
                    });
            }
            //选择文字主题
            $scope.SelectTextTheme = function (t) {
                $http
                    .post("http://crm.cike360.com/portal/index.php?r=background/select_word_theme",
                        JSON.stringify({
                            order_id: $scope.orderId,
                            word_theme_id: id
                        }))
                    .success(function (data) {
                        alert(data);
                        angular.forEach($scope.textThemeList,
                            function (i, x) {
                                if (i.id == t.id) {
                                    i.selected = !i.selected;
                                } else {
                                    i.selected = false;
                                }
                            });

                        $scope.curTextTheme = $scope.curTextTheme.id == t.id ? null : t;
                    })
                    .error(function (data, status, headers, config) {
                        alert("网络出现问题，请稍后重试！");
                    });
            }
            //验证
            $scope.TextThemeValidate = function () {
                if ($.trim($scope.textWords) == "") {
                    $scope.textThemeValidate.textWords = false;
                } else {
                    $scope.textThemeValidate.textWords = true;
                }
                if ($.trim($scope.textRemark) == "") {
                    $scope.textThemeValidate.textRemark = false;
                } else {
                    $scope.textThemeValidate.textRemark = true;
                }
                $scope.textThemeValidate.total = $scope.textThemeValidate.textWords &&
                    $scope.textThemeValidate.textRemark;
                return $scope.textThemeValidate.total;
            }

            //颜色主题
            $scope.OpenColorThemePop = function () {
                bombBox({
                    entrance: $('.theme_color_msgbox')
                });
            }
            //添加颜色主题
            $scope.AddColorTheme = function () {
                bombBox({
                    title: '添加主题颜色',
                    entrance: $('.color_theme_add_msgbox'),
                    initFun: function () {
                        $scope.colorName = null;
                        $scope.colorRemark = null;
                        $scope.mainColor = null;
                        $scope.secondColor = null;
                        $scope.thirdColor = null;
                    }
                });

                $scope.PopConfirm = function ($event) {
                    if ($scope.ColorThemeValidate()) {
                        $http
                            .post("http://crm.cike360.com/portal/index.php?r=background/insert_idea_color",
                                JSON.stringify({
                                    name: $scope.colorName,
                                    remark: $scope.colorRemark,
                                    token: $scope.token,
                                    main_color: $scope.mainColor,
                                    second_color: $scope.secondColor,
                                    third_color: $scope.thirdColor
                                }))
                            .success(function (data) {
                                alert(data);
                                $scope.colorThemeList.push({
                                    id: 1000,
                                    name: $scope.colorName,
                                    remark: $scope.colorRemark,
                                    main_color: $scope.mainColor,
                                    second_color: $scope.secondColor,
                                    third_color: $scope.thirdColor,
                                    selected: false
                                });
                                $($event.target).parents(".msgbox").hide();
                            })
                            .error(function (data, status, headers, config) {
                                alert("网络出现问题，请稍后重试！");
                            });
                    }
                }
            }
            //编辑颜色主题
            $scope.EditColorTheme = function (item) {
                bombBox({
                    title: '编辑主题颜色',
                    entrance: $('.color_theme_add_msgbox'),
                    initFun: function () {
                        $scope.colorName = item.name;
                        $scope.colorRemark = item.remark;
                        $scope.mainColor = item.main_color;
                        $scope.secondColor = item.second_color;
                        $scope.thirdColor = item.third_color;
                    }
                });
                $scope.PopConfirm = function ($event) {
                    if ($scope.ColorThemeValidate()) {
                        $http
                            .post("http://crm.cike360.com/portal/index.php?r=background/edit_idea_color",
                                JSON.stringify({
                                    name: $scope.colorName,
                                    remark: $scope.colorRemark,
                                    color_id: item.id,
                                    main_color: $scope.mainColor,
                                    second_color: $scope.secondColor,
                                    third_color: $scope.thirdColor
                                }))
                            .success(function (data) {
                                alert(data);
                                angular.forEach($scope.colorThemeList,
                                    function (i, x) {
                                        if (i.id == item.id) {
                                            i.name = $scope.colorName;
                                            i.remark = $scope.colorRemark;
                                            i.main_color = $scope.mainColor;
                                            i.second_color = $scope.secondColor;
                                            i.third_color = $scope.thirdColor;
                                        }
                                    });
                                if ($scope.curColorTheme.id == item.id) {
                                    $scope.curColorTheme.name = $scope.colorName;
                                    $scope.curColorTheme.remark = $scope.colorRemark;
                                    $scope.curColorTheme.main_color = $scope.mainColor;
                                    $scope.curColorTheme.second_color = $scope.secondColor;
                                    $scope.curColorTheme.third_color = $scope.thirdColor;
                                }
                                $($event.target).parents(".msgbox").hide();
                            })
                            .error(function (data, status, headers, config) {
                                alert("网络出现问题，请稍后重试！");
                            });
                    }
                }
            }
            //删除颜色主题
            $scope.DeleteColorTheme = function (id) {
                $http
                    .post("http://crm.cike360.com/portal/index.php?r=background/del_idea_color",
                        JSON.stringify({
                            color_id: id
                        }))
                    .success(function(data) {
                        alert(data);
                        angular.forEach($scope.colorThemeList,
                            function(i, x) {
                                if (i.id == id) {
                                    $scope.colorThemeList.splice(x, 1);
                                    return;
                                }
                            });
                        if ($scope.curColorTheme.id == id) {
                            $scope.curColorTheme = null;
                        }
                    })
                    .error(function(data, status, headers, config) {
                        alert("网络出现问题，请稍后重试！");
                    });
            }
            //删除已选颜色主题
            $scope.DeleteSelectedTextTheme = function (id) {
                $http
                    .post("http://crm.cike360.com/portal/index.php?r=background/del_idea_color_order_show",
                        JSON.stringify({
                            show_id: id
                        }))
                    .success(function (data) {
                        alert(data);
                        angular.forEach($scope.colorThemeList,
                            function (i, x) {
                                if (i.id == id) {
                                    i.selected = false;
                                    return;
                                }
                            });
                        $scope.curColorTheme = null;
                    })
                    .error(function (data, status, headers, config) {
                        alert("网络出现问题，请稍后重试！");
                    });
            }
            //选择颜色主题
            $scope.SelectColorTheme = function (t) {
                $http
                    .post("http://crm.cike360.com/portal/index.php?r=background/Select_theme_color",
                        JSON.stringify({
                            order_id: $scope.orderId,
                            theme_color_id: t.id
                        }))
                    .success(function(data) {
                        alert(data);
                        angular.forEach($scope.colorThemeList,
                            function (i, x) {
                                if (i.id == t.id) {
                                    i.selected = !i.selected;
                                } else {
                                    i.selected = false;
                                }
                            });

                        $scope.curColorTheme = $scope.curColorTheme.id==t.id ? null : t;
                    })
                    .error(function(data, status, headers, config) {
                        alert("网络出现问题，请稍后重试！");
                    });
            }
            //验证
            $scope.ColorThemeValidate = function () {
                if ($.trim($scope.colorName) == "") {
                    $scope.colorThemeValidate.colorName = false;
                } else {
                    $scope.colorThemeValidate.colorName = true;
                }
                if ($.trim($scope.colorRemark) == "") {
                    $scope.colorThemeValidate.colorRemark = false;
                } else {
                    $scope.colorThemeValidate.colorRemark = true;
                }
                if ($.trim($scope.mainColor) == "") {
                    $scope.colorThemeValidate.mainColor = false;
                } else {
                    $scope.colorThemeValidate.mainColor = true;
                }
                if ($.trim($scope.secondColor) == "") {
                    $scope.colorThemeValidate.secondColor = false;
                } else {
                    $scope.colorThemeValidate.secondColor = true;
                }
                if ($.trim($scope.thirdColor) == "") {
                    $scope.colorThemeValidate.thirdColor = false;
                } else {
                    $scope.colorThemeValidate.thirdColor = true;
                }
                $scope.colorThemeValidate.total = $scope.colorThemeValidate.colorName &&
                    $scope.colorThemeValidate.colorRemark &&
                    $scope.colorThemeValidate.mainColor &&
                    $scope.colorThemeValidate.secondColor &&
                    $scope.colorThemeValidate.thirdColor;
                return $scope.colorThemeValidate.total;
            }

            function echart() {
                var orderTotal = '{"feast":[],"other":[],"areap":[{"id":"3","name":"\u5ba4\u5916\u5e03\u7f6e","zheqian_total":0,"zhehou_total":0,"cost":0},{"id":"4","name":"\u7b7e\u5230\u533a\u5e03\u7f6e","zheqian_total":3300,"zhehou_total":3300,"cost":0},{"id":"5","name":"\u4eea\u5f0f\u533a\u5e03\u7f6e","zheqian_total":13000,"zhehou_total":13000,"cost":0},{"id":"6","name":"\u670d\u52a1\u4eba\u5458","zheqian_total":0,"zhehou_total":0,"cost":0},{"id":"7","name":"\u821e\u7f8e\u8bbe\u5907","zheqian_total":0,"zhehou_total":0,"cost":0},{"id":"9","name":"\u5a5a\u54c1","zheqian_total":0,"zhehou_total":0,"cost":0},{"id":"10","name":"\u5a5a\u793c\u8f66\u8f86","zheqian_total":0,"zhehou_total":0,"cost":0},{"id":"13","name":"\u6446\u4ef6\u9053\u5177","zheqian_total":0,"zhehou_total":0,"cost":0}],"non_area":{"zhehou_total":0,"cost":0},"total":{"price":16300,"cost":0,"profit":16300}}';
                orderTotal = JSON.parse(orderTotal);
                var allTotal = orderTotal.total.price.toFixed(0);
                var pieName = null;
                //var pieName = ['婚宴'];
                var outData = [];
                var inData = [];
                //计算婚宴
                var feast_total = 0;
                $.each(orderTotal.feast, function (index, value) {
                    feast_total += parseFloat(value.zhehou_total);
                });
                //计算未分配
                var non_area_total = orderTotal.non_area.zhehou_total;
                //构造外圈呈现内容
                outData.push({
                    value: feast_total,
                    name: '婚宴',
                    itemStyle: {
                        normal: {
                            color: '#dc69aa'
                        }
                    }
                });
                $.each(orderTotal.areap, function (index, value) {
                    //pieName.push(value.name);
                    outData.push({
                        value: value.zhehou_total,
                        name: value.name
                    });
                });
                //pieName.push("待分配产品", '服务人员', '场地布置', '灯光音响视频');
                outData.push({
                    value: non_area_total,
                    name: '待分配产品',
                    itemStyle: {
                        normal: {
                            color: '#8d98b3'
                        }
                    }
                });

                //构造内圈呈现内容
                inData.push({
                    value: feast_total,
                    name: '婚宴',
                    itemStyle: {
                        normal: {
                            color: '#dc69aa'
                        }
                    }
                });
                inData.push({
                    value: 0,
                    name: '服务人员',
                    itemStyle: {
                        normal: {
                            color: '#2094e6'
                        }
                    }
                });
                inData.push({
                    value: 16300,
                    name: '场地布置',
                    itemStyle: {
                        normal: {
                            color: '#ecd70f'
                        }
                    }
                });
                inData.push({
                    value: 0,
                    name: '灯光音响视频',
                    itemStyle: {
                        normal: {
                            color: '#0fec63'
                        }
                    }
                });

                var myChart = echarts.init(document.getElementById('main'));
                option = {
                    tooltip: {
                        trigger: 'item',
                        formatter: "{a} <br/>{b}: {c} ({d}%)"
                    },
                    legend: {
                        orient: 'horizontal',
                        y: 'bottom',
                        x: '20',
                        itemWidth: 15,
                        textStyle: {
                            fontSize: 10,
                            color: '#999',
                        },
                        data: pieName
                    },
                    series: [{
                        name: '类别',
                        type: 'pie',
                        selectedMode: 'single',
                        radius: ['30%', '50%'],
                        itemStyle: {
                            normal: {
                                label: {
                                    show: false,
                                    normal: {
                                        position: 'inner'
                                    }
                                },
                                labelLine: {
                                    show: false
                                }
                            }
                        },
                        data: inData
                    },
                        {
                            name: '总价',
                            type: 'pie',
                            selectedMode: '',
                            radius: ['0', '32%'],
                            itemStyle: {
                                normal: {
                                    label: {
                                        show: true,
                                        position: 'center',
                                        textStyle: {
                                            fontSize: '15',
                                            fontWeight: 'bold',
                                            color: '#333',
                                        },
                                        formatter: function (data) {
                                            return data.name + '\n' + data.value;
                                        },
                                    },
                                    labelLine: {
                                        show: false
                                    }
                                }
                            },
                            labelLine: {
                                normal: {
                                    show: false
                                }
                            },
                            data: [{
                                value: '￥' + allTotal,
                                name: '总价TOTAL',
                                itemStyle: {
                                    normal: {
                                        color: '#fff'
                                    }
                                }
                            }, ]
                        },
                        {
                            name: '区域',
                            type: 'pie',
                            radius: ['54%', '73%'],
                            itemStyle: {
                                normal: {
                                    label: {
                                        show: false
                                    },
                                    labelLine: {
                                        show: false
                                    }
                                }
                            },
                            data: outData
                        }
                    ]
                };
                myChart.setOption(option);
            }
            /*打印弹框*/
            var printMsg = {
                init: function () {
                    printMsg.initPosition();
                    $('.print_btn').on('click', printMsg.show)
                    $('.print_msgbox').on('click', '.mask', printMsg.close)
                    $('.print_msgbox').on('click', '.add_mail', printMsg.addSureaddMail)
                    $('.print_msgbox').on('click', '.sure_btn', printMsg.addSure)
                    $('.print_msgbox').on('click', '.del', printMsg.del)
                    $('.print_msgbox').on('click', '.send', printMsg.sendMail)
                },
                initPosition: function () {
                    var winWidth = document.body.clientWidth;
                    var left = (winWidth - 700) / 2 + 'PX';
                    $('.print_msgbox .print_msg').css('left', left);
                },
                show: function () {
                    $('.print_msgbox').show();
                },
                close: function () {
                    $('.print_msgbox').hide();
                },
                addSureaddMail: function () {
                    $('.mail_list').prepend('<li class="clearfix new_item"><div class="left"><input type="text"></div><div class="left"><div><button class = "sure_btn" > 确定 </button> </div></div> </li>')
                },
                addSure: function () {
                    var _newMail = $(this).parents('.new_item').find('input').val();
                    if ($.trim(_newMail) != '') {
                        $(this).parents('.new_item').find('input').after('<p class="name">' + _newMail + '</p></div>');
                        $(this).parents('.new_item').find('input').remove();
                        $(this).after('<span class="left send">发送报价单</span><span class="left sep">|</span><span class="left del">删除</span>');
                        $(this).remove();
                    }
                    /*此处ajax 新增邮箱*/
                },
                del: function () {
                    $(this).parents('li').remove();
                    /*此处ajax 删除邮箱*/
                },
                sendMail: function () {
                    /*发送报价单相关操作*/
                }
            };
            /*侧边导航*/
            var sideBar = {
                init: function () {
                    $('.sidebar_list').on('click', '.name', sideBar.expand)
                    $('.side_subbar_list').on('click', 'li', sideBar.scroll)
                    $('.sidebar_item').on('mouseover', '.addbtn_box', sideBar.addHover).on('mouseout', '.addbtn_box', sideBar.addout)
                    $('#print_area').on('mouseover', '.addbtn_box', sideBar.addHover).on('mouseout', '.addbtn_box', sideBar.addout)
                    $('.sidebar_list').on('click', '.upload_item', sideBar.upload)
                    $('#print_area').on('click', '.upload_item', sideBar.upload)
                    $('.add_msgbox').on('click', '.sure_btn', sideBar.uploadSure)
                    $('.add_msgbox').on('click', '.mask', sideBar.uploadClose)
                    $('.add_msgbox').on('click', '.close', sideBar.uploadClose)
                },
                expand: function () {
                    if ($(this).prev().hasClass('up')) {
                        $(this).prev().removeClass('up');
                        $(this).parents('li').find('.side_subbar_list').hide();
                    } else {
                        $(this).prev().addClass('up');
                        $(this).parents('li').find('.side_subbar_list').show();
                    }
                },
                scroll: function () {
                    var _dataid = $(this).data('id');
                    var _offsetTop = $("#" + _dataid).offset().top - 60;
                    $(window).scrollTop(_offsetTop);
                    $('li').removeClass('active');
                    $(this).addClass('active');
                },
                addHover: function () {
                    $(this).find('.hovercon_box').show();
                },
                addout: function () {
                    $(this).find('.hovercon_box').hide();
                },
                upload: function () {
                    var winWidth = document.body.clientWidth;
                    var left = (winWidth - 700) / 2 + 'px';
                    var dataid;
                    if ($(this).parents(".addbtn_box").hasClass('intable')) {
                        dataid = $(this).parents('table').data('id');
                    } else {
                        console.log("dddddd")
                        dataid = $(this).parents('.sidebar_item').data('id');
                    }
                    console.log(dataid)
                    $('.add_msgbox .add_msg').css('left', left);
                    $('.add_msgbox').attr('data-id', dataid);
                    $('.add_msgbox').show();
                },
                uploadSure: function () {
                    var _inname = $("#in_name").val(),
                        _inunit = $("#in_unit").val(),
                        _inprice = $("#in_price").val(),
                        _innum = $("#in_num").val(),
                        _indesc = $("#in_desc").val(),
                        _inbz = $("#in_bz").val(),
                        _sel = $("select").find("option:selected").text();
                    var dataid = $('.add_msgbox').attr('data-id');
                    var html = '';
                    var lastid = $('.option_table').find('#' + dataid).find('tbody tr:last').attr('id');
                    var newid = lastid.substring(0, lastid.length - 1) + (Number(lastid.substr(lastid.length - 1)) + 1);
                    console.log(dataid)
                    html = '<tr id="' + newid + '"><td><div>' + _inname + '</div></td>'
                              + '<td><img class="list_img" src="http://file.cike360.com/upload/set/set38888/a水牌 (12)_xs.jpg"></td>'
                              + '<td><div>' + _inprice + '</div></td>'
                              + '<td><div>' + _inunit + '</div></td>'
                              + '<td><div>' + _innum + '</div></td>'
                              + '<td><div class="list_remark">' + _inbz + '</div></td>'
                              + '<td><div>' + _inprice * _innum + '</div></td>'
                              + '<td class="option_item">'
                                + '<div class="clearfix">'
                                   + '<span class="left editbtn">编辑</span>'
                                   + '<span class="left sep">|</span>'
                                   + '<span class="left delbtn">删除</span>'
                                 + '</div></td></tr>'
                    $('.option_table').find('#' + dataid).find('tbody').append(html);
                    /*此处ajax 删除邮箱*/
                    $(this).parents('.add_msgbox').hide();
                },
                uploadClose: function () {
                    $('.add_msgbox').hide();
                }
            }
            /*表格操作*/
            var tableOption = {
                init: function () {
                    $('#print_area').on('click', '.option_item .delbtn', tableOption.del)
                    $('#print_area').on('click', '.option_item .editbtn', tableOption.edit)
                    $('.edit_msgbox').on('click', '.sure_btn', tableOption.sureBtn)
                    $('.edit_msgbox').on('click', '.mask', tableOption.close)
                    $('.edit_msgbox').on('click', '.close', tableOption.close)
                    $('#print_area').on('click', '.list_img', tableOption.imgMsg)
                    $('body').on('click', '.imgmsgbox .mask', tableOption.imgClose)
                },
                del: function () {
                    $(this).parents('tr').remove();
                    /*此处ajax 删除邮箱*/
                },
                edit: function () {
                    var winWidth = document.body.clientWidth;
                    var left = (winWidth - 700) / 2 + 'px';
                    $('.edit_msgbox .edit_msg').css('left', left);
                    $('.edit_msgbox').show();
                    var dataid = $(this).parents('tr').attr('id');
                    $('.edit_msgbox').attr('data-id', dataid);
                },
                sureBtn: function () {
                    var _inprice = $(".edit_msgbox [data-flag=price]").val(),
                        _innum = $(".edit_msgbox [data-flag=num]").val(),
                        _inbz = $(".edit_msgbox [data-flag=remark]").val();
                    var dataid = $(this).parents('.edit_msgbox').attr('data-id');
                    console.log(dataid);
                    $('.option_table').find('#' + dataid).find('[data-flag=price] div').html(_inprice);
                    $('.option_table').find('#' + dataid).find('[data-flag=num] div').html(_innum);
                    $('.option_table').find('#' + dataid).find('[data-flag=remark] div').html(_inbz);
                    /*此处ajax 删除邮箱*/

                    $(this).parents('.edit_msgbox').hide();

                },
                close: function () {
                    $('.edit_msgbox').hide();
                },
                imgMsg: function () {
                    var _src = $(this).attr('src');
                    var winWidth = document.body.clientWidth;
                    var left = (winWidth - 500) / 2 + 'px';
                    var html = '<div class="msgbox imgmsgbox">'
                                + '<div class="mask"></div>'
                                + '<div class="imgbox" style="left:' + left + '"><img src=' + _src + '></div>'
                              + '</div>'
                    $("body").append(html);
                },
                imgClose: function () {
                    $('.imgmsgbox').hide();
                }
            }
            /*效果图滑动调用*/
            var swip = new Swiper($(".effect_swiper"), {
                slidesPerView: 'auto',
                initialSlide: 0,
                autoPlay: false,
                loop: false,
                nextButton: '.swiper-button-next',
                prevButton: '.swiper-button-prev',
            });

            //仪式区效果图
            //打开选择弹出
            $scope.OpenYsqPop = function () {
                //todo ajax??

                bombBox({
                    entrance: $('.ysq_msgbox')
                });
            }

            //添加效果图，此处执行操作
            $scope.AddYsq = function () {
                bombBox({
                    title: '新增效果图',
                    entrance: $('.ysq_add_msgbox'),
                    initFun: function () {
                        $scope.ysqImg = null;
                        $scope.ysqDesc = null;
                    }
                });

                $scope.PopConfirmAction = function () {
                    //todo ajax
                    $scope.ysqList.push({
                        id: 1000,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: $scope.ysqImg,
                        desc: $scope.ysqDesc,
                        checked: false
                    });
                }
            }

            //编辑效果图，此处执行操作
            $scope.EditYsq = function (item) {
                bombBox({
                    title: '编辑效果图',
                    entrance: $('.color_theme_add_msgbox'),
                    initFun: function () {
                        $scope.ysqImg = item.img;
                        $scope.ysqDesc = item.desc;
                    }
                });
                $scope.PopConfirmAction = function () {
                    //todo ajax
                    angular.forEach($scope.ysqList,
                        function (i, t) {
                            if (i.id == item.id) {
                                i.img = $scope.ysqImg;
                                i.desc = $scope.ysqDesc;
                            }
                        });
                    angular.forEach($scope.curYsqList,
                        function (i, t) {
                            if (i.id == item.id) {
                                i.img = $scope.ybqImg;
                                i.desc = $scope.ybqDesc;
                            }
                        });
                }
            }

            //删除效果图
            $scope.DeleteYsq = function (id) {
                //todo ajax
                angular.forEach($scope.ysqList,
                    function (i, t) {
                        if (i.id == id) {
                            $scope.ysqList.splice(t, 1);
                        }
                    });
                angular.forEach($scope.curYsqList,
                    function (i, t) {
                        if (i.id == id) {
                            $scope.curYsqList.splice(t, 1);
                        }
                    });
            }

            //选择效果图
            $scope.SelectYsq = function (item) {
                angular.forEach($scope.ysqList,
                    function (i, t) {
                        if (i.id == item.id) {
                            i.checked = !i.checked;
                        }
                    });
                angular.forEach($scope.curYsqList,
                    function (i, t) {
                        if (i.id == item.id) {
                            $scope.curYsqList.splice(t, 1);
                            return;
                        }
                    });
                $scope.curYsqList.push(item);
            }

            //迎宾区效果图
            //打开选择弹出
            $scope.OpenYbqPop = function () {
                //todo ajax??

                bombBox({
                    entrance: $('.ybq_msgbox')
                });
            }

            //添加效果图，此处执行操作
            $scope.AddYbq = function () {
                bombBox({
                    title: '新增效果图',
                    entrance: $('.ybq_add_msgbox'),
                    initFun: function () {
                        $scope.ybqImg = null;
                        $scope.ybqDesc = null;
                    }
                });

                $scope.PopConfirmAction = function () {
                    //todo ajax
                    $scope.ybqList.push({
                        id: 1000,
                        title: "上传个人资料图片",
                        must: "可选",
                        img: $scope.ybqImg,
                        desc: $scope.ybqDesc,
                        checked: false
                    });
                }
            }

            //编辑效果图，此处执行操作
            $scope.EditYbq = function (item) {
                bombBox({
                    title: '编辑效果图',
                    entrance: $('.ybq_add_msgbox'),
                    initFun: function () {
                        $scope.ybqImg = item.img;
                        $scope.ybqDesc = item.desc;
                    }
                });
                $scope.PopConfirmAction = function () {
                    //todo ajax
                    angular.forEach($scope.ybqList,
                        function (i, t) {
                            if (i.id == item.id) {
                                i.img = $scope.ybqImg;
                                i.desc = $scope.ybqDesc;
                            }
                        });

                    angular.forEach($scope.curYbqList,
                        function (i, t) {
                            if (i.id == item.id) {
                                i.img = $scope.ybqImg;
                                i.desc = $scope.ybqDesc;
                            }
                        });
                }
            }

            //删除效果图
            $scope.DeleteYbq = function (id) {
                //todo ajax
                angular.forEach($scope.ybqList,
                    function (i, t) {
                        if (i.id == id) {
                            $scope.ybqList.splice(t, 1);
                        }
                    });
                angular.forEach($scope.curYbqList,
                    function (i, t) {
                        if (i.id == id) {
                            $scope.curYbqList.splice(t, 1);
                        }
                    });
            }

            //选择效果图
            $scope.SelectYbq = function (item) {
                angular.forEach($scope.ybqList,
                    function (i, t) {
                        if (i.id == item.id) {
                            i.checked = !i.checked;
                        }
                    });
                angular.forEach($scope.curYbqList,
                    function (i, t) {
                        if (i.id == item.id) {
                            $scope.curYbqList.splice(t, 1);
                            return;
                        }
                    });
                $scope.curYbqList.push(item);
                if (swiper) {
                    swiper.update(true);
                } else {
                    alert("asad")
                }
            }

            /*echart渲染*/
            echart();
            /*打印弹框初始化*/
            printMsg.init();
            /*侧边导航初始化*/
            sideBar.init();
            /*表格操作初始化*/
            tableOption.init();
        }
    ])
.filter("inArray",
    function () {
        return function (array, id) {
            angular.forEach(array,
                function (i, t) {
                    if (i.id == id) {
                        return true;
                    }
                });
            return false;
        }
    });